<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
      <el-breadcrumb-item :to="{ path: '/homeIndex' }">
        <i class="el-icon-s-promotion"></i> 后台管理
      </el-breadcrumb-item>
      <el-breadcrumb-item>餐品列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-divider></el-divider>

    <el-card style="float: left ;width: 100%; height: 100%;margin: 10px">
      <el-divider content-position="left">餐品表格</el-divider>

      <!--    添加餐品按钮-->
      <el-button type="warning" icon="el-icon-edit" @click="openAddNewDialog()">添加餐品</el-button>
      <!--弹出的新增餐品的表格-->
      <el-dialog title="新增餐品" :visible.sync="addNewFormVisible">
        <el-form :model="addNewForm" :rules="rules" ref="addNewForm">
          <el-form-item label="餐品名称" prop="name" :label-width="formLabelWidth">
            <el-input v-model="addNewForm.name"></el-input>
          </el-form-item>
          <el-form-item label="餐品价格" prop="price" :label-width="formLabelWidth">
            <el-input v-model="addNewForm.price"></el-input>
          </el-form-item>
          <el-form-item label="餐品图片" :label-width="formLabelWidth">
            <el-upload
              action="api/employee/upload"
              :on-success="handleAvatarSuccess"
              :show-file-list="false"
              v-model="addNewForm.url"
              list-type="picture-card"
            >
              <img v-if="imageUrl" :src="imageUrl" class="avatar" />

              <i v-else class="el-icon-plus"></i>
            </el-upload>
          </el-form-item>
        </el-form>

        <!-- 新增餐品的按钮       -->
        <div slot="footer" class="dialog-footer" style="text-align: center">
          <el-button type="warning" @click="addNewSubmitForm">新增餐品</el-button>
          <el-button @click="addNewFormVisible = false">取 消</el-button>
        </div>
      </el-dialog>

      <!--    弹出的修改餐品的表单-->

      <el-dialog title="修改餐品" :visible.sync="dialogVisible">
        <el-form :model="editForm" :rules="rules" ref="editForm" label-width="80px">
          <el-form-item label="菜品名称" prop="name">
            <el-input v-model="editForm.name" type="text"></el-input>
          </el-form-item>
          <el-form-item label="菜品价格" prop="price">
            <el-input v-model="editForm.price" type="text"></el-input>
          </el-form-item>
          <!-- <el-form-item label="菜品图片">
            <template slot-scope="scope">
              <img :src="scope.row.url" style="width:55%"  width="300" height="300"  />
            </template>
          </el-form-item> -->
          <el-form-item label="上传图片">
            <el-upload
              action="api/employee/upload"
              :on-success="edithandleSuccess"
              :show-file-list="false"
              v-model="editForm.url"
              :limit="1"
              list-type="picture"
            >
              <i class="el-icon-plus"></i>
              <img v-if="imageUrl2" :src="imageUrl2" class="avatar" />
            </el-upload>
          </el-form-item>
          <el-form-item>
            <el-button type="success" @click="updateForm('editForm')">修改菜品</el-button>
          </el-form-item>
        </el-form>
      </el-dialog>

      <!--    餐品列表-->
      <el-table
        ref="multipleTable"
        tooltip-effect="dark"
        style="width: 100%"
        border
        :data="this.foodArr"
      >
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column type="index" label="编号" align="center" width="50"></el-table-column>
        <el-table-column prop="name" label="餐品名称" header-align="center"></el-table-column>
        <el-table-column prop="price" label="餐品价格" width="100" header-align="center"></el-table-column>
        <el-table-column label="餐品图片" width="100" header-align="center">
          <!--如果表格的列显示的内容不是来自于数组里面对象的属性,而是显示一些
          其它的标签组成的内容时,需要自定义列-->
          <!--自定义列 必须添加template标签-->
          <template slot-scope="scope">
            <div class="demo-type">
              <img :src="scope.row.url" style="width:55%" size="50" />
            </div>
          </template>
        </el-table-column>

        <!--操作列-->
        <el-table-column label="操作" width="200" align="center">
          <template slot-scope="scope">
            <el-button size="mini" type="warning" @click="openEditDialog(scope.row.id)">编辑</el-button>
            <el-button
              size="mini"
              type="info"
              icon="el-icon-delete"
              @click="openDeleteConfirm(scope.row.id)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          background
          :page-sizes="[5, 10, 20, 50]"
          :page-size="pageSize"
          :current-page="pageNum"
          layout="total, sizes, prev, pager, next"
          @next-click="nextPage"
          @prev-click="prevPage"
          @size-change="sizeChange"
          @current-change="currentPage"
          :total="total"
        ></el-pagination>
      </div>
    </el-card>
  </div>
</template>

<script>
export default {
  name: "FoodListView",
  data: function() {

    return {
      
      imageUrl: "",
      imageUrl2: "",
      pageNum: 1,
      pageSize: 5,
      total: 0,
      formLabelWidth: "120px",
      dialogVisible: false,
      editdialogVisible: false,
      foodArr: [],
      food: [],
      addNewFormVisible: false,
      dialogFormVisible: false,
      rules: {
        name: [{ required: true, message: "请输入餐品名称", trigger: "blur" }],
        price: [
          { required: true, message: "请输入餐品价格", trigger: "blur" },
          {
            pattern: /^([1-9]\d*(\.\d{1,2})?)|(^0(\.\d{1,2})?$)/,
            message: "价格必须是小数",
            trigger: "blur"
          }
        ]
      },
      addNewForm: { name: "", price: "", url: "" },
      editForm: { id: "", name: "", price: "", url: "" }
    };
  },
  methods: {
    prevPage(val) {
      //点击上一页触发
      this.pageNum = val;
      this.loadFoodList();
    },
    nextPage(val) {
      //点击下一页触发
      this.pageNum = val;
      this.loadFoodList();
    },
    sizeChange(val) {
      //val每一页条数
      this.pageNum = 1;
      this.pageSize = val;
      this.loadFoodList();
    },
    currentPage(val) {
      this.pageNum = val;
      this.loadFoodList();
    },
    queryCondition() {
      thispageNum = 1;
      this.loadFoodList();
    },
    loadFoodList() {
      //带分页参数pageNum,pageSize
      let pageParam = {};
      pageParam.pageNum = this.pageNum;
      pageParam.pageSize = this.pageSize;
      pageParam.param = this.orderParam;

      this.$axios.post("api/food/queryFood", pageParam).then(res => {
        if (res.data.code == 200) {
          let pageData = res.data.data;

          this.foodArr = pageData.list;
          //给分页参数赋值
          this.total = pageData.total;
          this.pageNum = pageData.pageNum;
          this.pageSize = pageData.pageSize;
        }
      });
    },

    updateForm() {
      // 验证必填项
      if (!this.editForm.name || !this.editForm.price) {
        this.$message.error("请填写所有必填项！");
        return;
      }
      //添加图片
      this.editForm.url = this.imageUrl2;
      let Food = this.editForm;
      this.$axios.post("api/food/updateFood", Food).then(res => {
        if (res.data.code == 200) {
          this.loadFoodList();
          this.dialogVisible = false;
          this.$message({ message: "修改餐品成功！", type: "success" });
          location.reload();
        } else {
          this.$message.error(responseBody.message);
        }
      });
    },

    edithandleSuccess(res, file) {
      this.imageUrl2 = res.data;
      console.log("re:{}", res);
    },

    handleAvatarSuccess(res, file) {
      this.imageUrl = res.data;
      console.log("re:{}", res);
    },
    openAddNewDialog() {
      this.addNewFormVisible = true;
    },
    openDeleteConfirm(val) {
      this.$confirm("确认删除吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.handleDelete(val);
        })
        .catch(() => {});
    },
    openEditDialog(val) {
      this.dialogVisible = true;
      this.$axios.get("api/food/queryById/" + val).then(res => {
        if (res.data.code == 200) {
          this.editForm = res.data.data; // 使用this来引用data()中的originalForm
          this.imageUrl2 = this.editForm.url; // 可以直接从this.originalForm中获取
        }
      });
    },

    handleDelete(val) {
      //餐品删除方法
      console.log(val); // 在这里打印val
      this.$confirm("确认删除吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.$axios.get("api/food/deleteFood/" + val).then(res => {
          if (res.data.code == 200) {
            //发删除请求
            this.$message.success("删除成功!");
            this.loadFoodList();
          }
        });
      });
    },

    addNewSubmitForm() {
      // 验证必填项
      if (!this.addNewForm.name || !this.addNewForm.price) {
        this.$message.error("请填写所有必填项！");
        return;
      }
      this.addNewForm.url = this.imageUrl;
      let Food = this.addNewForm;
      this.$axios.post("api/food/addFood", Food).then(res => {
        if (res.data.code == 200) {
          this.$message.success("提交成功!");
          this.loadFoodList();
          this.addNewFormVisible = false;
          location.reload();
        } else {
          this.$message.error(res.data.msg);
        }
      });
    },
    queryById() {
      this.$axios.get("api/food/queryById/" + this.id).then(res => {
        if (res.data.code == 200) {
          const originalForm = res.data.data;
          this.imageUrl2 = originalForm.url;
          this.editForm = originalForm;
        }
      });
    },

  },
  mounted() {
    this.loadFoodList();
    // if (this.$route.query.id) {
    //   this.id = this.$route.query.id;
    //   this.queryById();
    // } else {
    //   console.error("The id is undefined.");
    // }
  },
  created() {}
};
</script>

<style scoped>
</style>